package com.beep.erp.common.base;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.beep.erp.R;
import com.beep.erp.common.dialog.DialogControl;
import com.beep.erp.common.utils.TUtil;
import com.beep.erp.common.widget.tips.DefaultTipsHelper;
import com.beep.erp.common.widget.tips.TipsHelper;
import com.trello.rxlifecycle.components.support.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * FileName: BaseFragment
 * description:
 * Author: jolly
 * Date: 2016/9/12
 */
public abstract class BaseFragment<P extends BasePresenter, M> extends RxFragment implements DialogControl {

    protected View rootView;
    public P mPresenter;
    public M mModel;
    private Unbinder unbinder;

    private boolean _isVisible;
    private MaterialDialog _waitDialog;

    protected abstract int getLayoutId();

    protected abstract void initView(View view);

    // 标志位 标志已经初始化完成。
    protected boolean isPrepared;
    //标志位 fragment是否可见
    protected boolean isVisible;
    protected TipsHelper mTipsHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
            unbinder = ButterKnife.bind(this, rootView);
            initView(rootView);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        unbinder = ButterKnife.bind(this, rootView);
        //初始化view的各控件
        isPrepared = true;
        lazyLoad();
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);
        if (this instanceof BaseView) mPresenter.setVM(this, mModel);
    }
    protected TipsHelper createTipsHelper(View view) {
        return new DefaultTipsHelper(getContext(), view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null)
            mPresenter.onDestroy();

       // unbinder.unbind();
    }


    protected ActionBar getActionBar() {
        return ((AppCompatActivity) getActivity()).getSupportActionBar();
    }

    /**
     * Fragment数据的懒加载
     *frahment从不可见到完全可见的时候，会调用该方法
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }
    protected void onVisible() {
        lazyLoad();
    }

    /**
     * 懒加载的方法,在这个方法里面我们为Fragment的各个组件去添加数据
     */
    protected abstract void lazyLoad();

    protected void onInvisible() {
    }

    @Override
    public MaterialDialog showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    @Override
    public MaterialDialog showWaitDialog(int resid) {
        return showWaitDialog(getString(resid));
    }

    @Override
    public MaterialDialog showWaitDialog(String text) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog= new  MaterialDialog.Builder(getActivity())
                        .progress(true, 0)
                        .progressIndeterminateStyle(false)
                        .build();
            }
            if (_waitDialog != null) {
                _waitDialog.setContent(text);
                _waitDialog.show();
            }
            return _waitDialog;
        }
        return null;
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean onBackPressed() {
        return false;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
